// page_two/suojadd/suojadd.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '索记索票',
    buttonClicked: false,
    khname: '',
    fwname: '',
    fynr: '',
    fyList:[
      {'value': '住宅管理费'},
      {'value': '垃圾费'},
      {'value': '电梯费'},
      {'value': '公摊电费'},
    ],
    startdate: '请选择开始时间',
    enddate: '请选择结束时间',
    total: '',
    photo: [],
    wuphoto: ''
  },
  // 客户名称
  khname(e) {
    this.setData({
      khname: e.detail.value
    })
  },
  // 房屋名称
  fwname(e) {
    this.setData({
      fwname: e.detail.value
    })
  },
  //多选
  userCheck:function(e){
    let index = e.currentTarget.dataset.id;//获取用户当前选中的索引值
    let checkBox = this.data.fyList;
    if (checkBox[index].checked){
      this.data.fyList[index].checked = false;
    }else{
      this.data.fyList[index].checked = true;
    }
    this.setData({ fyList: this.data.fyList})
 
    //返回用户选中的值
    let value = checkBox.filter((item,index)=>{
      return item.checked == true;
    })
    console.log(value)
    let fyitem = ''
    value.forEach((item, index) => {
      fyitem += item.value + ','
    })
    this.setData({
      fynr: fyitem
    })
  },
  // 开始时间
  startdate: function (e) {
    this.setData({
      startdate: e.detail.value
    })
  },
  // 结束时间
  enddate: function (e) {
    if (new Date(e.detail.value).getTime() < Date.parse(new Date())) {
      wx.showToast({
        title: '结束时间需晚于当前时间',
        icon: 'none'
      })
      this.setData({
        enddate: '请选择结束时间'
      })
    } else {
      this.setData({
        enddate: e.detail.value
      })
    }
  },
  // 总额
  total(e) {
    this.setData({
      total: e.detail.value
    })
  },
  // 上传
  chuan(e) {
    var that = this
    var pics = [];
    var wuphoto = this.data.wuphoto;
    this.myChooseImage(9).then(res => {
      const tempFilePaths = res.tempFilePaths
      for (var i = 0; i < tempFilePaths.length; i++) {
        pics.push(tempFilePaths[i])
      }
      if ((pics.length) + (that.data.photo.length) > 9) {
        wx.showToast({
          title: '最多可上传' + 9 + "张",
          icon: 'none'
        })
      } else {
        for (var i = 0; i < tempFilePaths.length; i++) {
          (function (i) { // 注意这里是形参
            setTimeout(function () {
              that.duoupimg(tempFilePaths[i]).then(res => {
                wx.showLoading({
                  title: '上传图片中...',
                });
                var data = JSON.parse(res.data)
                var urls = data.httpUrl + data.imgUrl
                wuphoto = wuphoto.concat(data.imgUrl + ",")
                that.setData({
                  photo: that.data.photo.concat(urls),
                  wuphoto: wuphoto
                });
                if (i == tempFilePaths.length - 1) {
                  wx.hideLoading();
                }
              })
            }, 800 * i); // 还是每秒执行一次，不是累加的
          })(i) // 注意这里是实参，这里把要用的参数传进去
        }
      }
    })
  },
  // 差掉
  chadiao(e) {
    var index = e.currentTarget.dataset.index
    var photo = this.data.photo;
    var wuphoto = this.data.wuphoto;
    var dd = wuphoto.split(",")
    console.log(dd)
    photo.splice(index, 1)
    dd.splice(index, 1)
    var rr = dd.join(",")
    this.setData({
      photo: photo,
      wuphoto: rr
    })
  },
  fangda(e) {
    var img = e.currentTarget.dataset.img
    var list = e.currentTarget.dataset.list
    wx.previewImage({
      current: img,
      urls: list
    })
  },
  // 验证
  ti() {
    if (this.data.khname == '') {
      wx.showToast({
        title: '请输入客户名称',
        icon: 'none'
      })
    } else if (this.data.fwname == '') {
      wx.showToast({
        title: '请输入房屋名称',
        icon: 'none'
      })
    } else if (this.data.fynr == '') {
      wx.showToast({
        title: '请选择费用项目',
        icon: 'none'
      })
    } else if (this.data.startdate == '请选择开始时间') {
      wx.showToast({
        title: '请选择开始时间',
        icon: 'none'
      })
    } else if (this.data.enddate == '请选择结束时间') {
      wx.showToast({
        title: '请选择结束时间',
        icon: 'none'
      })
    } else if (this.data.total == '') {
      wx.showToast({
        title: '请输入总额',
        icon: 'none'
      })
    } else if (this.data.photo.length == 0) {
      wx.showToast({
        title: '请上传图片',
        icon: 'none'
      })
    } else {
      this.que()
    }
  },
  // 添加索记索票
  que() {
    let that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'Besall/TicketsReportAdd', {
      shopId: wx.getStorageSync('shopId'),
      name: that.data.khname,
      homename: that.data.fwname,
      pricename: that.data.fynr,
      startime: that.data.startdate,
      endtime: that.data.enddate,
      price: that.data.total,
      images: that.data.wuphoto
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(function () {
          wx.navigateBack({
            delta: 0
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})